package com.circle.contrller;

import com.circle.model.entity.DccInternalFileEntity;
import com.circle.model.module.DbDao;
import com.circle.model.module.log.LoggerABC;
import com.circle.model.module.log.LoggerBuilder;
import com.circle.model.util.DbDaoFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class InternalFileController {

    public InternalFileController() {

    }

    public void search() {

    }

    public List<Map<String, Object>> search(String deptid, String deptName,String docType,String specNo) {
        LoggerABC loggerABC = LoggerBuilder.build();
        try{
            DbDao dbDao = DbDaoFactory.getInstance();
            StringBuilder sql = new StringBuilder("select spec_no,title,rev,owner_name,owner_no,dept_name,deptid,doc_type,id from dcc_internal_file where nodeid='F'");

            // 存储参数
            List<String> conditions = new ArrayList<>();

            // 动态添加条件
            if (deptid != null && !deptid.trim().isEmpty()) {
                conditions.add("deptid like '%" + deptid + "%'");
            }

            if (deptName != null && !deptName.trim().isEmpty()) {
                conditions.add("dept_name like '%" + deptName + "%'");
            }

            if (docType != null && !docType.trim().isEmpty()) {
                conditions.add("doc_type like '%" + docType + "%'");
            }

            if (specNo != null && !specNo.trim().isEmpty()) {
                conditions.add("spec_no like '%" + specNo + "%'");
            }

            // 如果有条件，添加到SQL中
            if (!conditions.isEmpty()) {
                sql.append(" and (").append(String.join(" and ", conditions)).append(")");
            }

            loggerABC.logMessage(LoggerABC.DEBUG,"Final SQL: " + sql);

            // 执行查询
            List<Map<String, Object>> results = dbDao.executeQuery(sql.toString());

            return results;
        }catch (Exception e){
            loggerABC.setException(e);
            loggerABC.logMessage(LoggerABC.ERROR,"Error occurred during search: " + e.getMessage());
        }
        return null;
    }

}
